package com.beilang.base.codelookup.manager.impl;

import java.util.ArrayList;
import java.util.List;

import com.beilang.base.cache.CacheManager;
import com.beilang.base.codelookup.config.CodeLookupBean;
import com.beilang.base.codelookup.config.CodeLookupConfig;
import com.beilang.base.codelookup.dao.CodeLookupDAO;
import com.beilang.base.codelookup.exception.CodeLookupException;
import com.beilang.base.codelookup.manager.CodeLookupMgr;
import com.beilang.base.codelookup.model.CodeModel;


public class CodeLookupMgrImpl implements CodeLookupMgr {

	private static CodeLookupDAO dao = null;

	private CodeLookupDAO getDAOInstance() {
		if (dao != null)
			return dao;
		dao = new CodeLookupDAO();
		return dao;
	}

	// without filter
	public List getCodeList(String codeType,boolean requireEmpty) throws CodeLookupException {
		List list = (List) CacheManager.getInstance().getFromCache(codeType);
		if (list != null){
            if(requireEmpty&&list.size()>0){
                List l=new ArrayList();
                CodeModel codeModel=new CodeModel();
                codeModel.setCodeId("");
                codeModel.setCodeDesc("");
                l.add(codeModel);
                l.addAll(list);
            return l;
            }
            return list;
        }

		if (!codeType.startsWith("CODE")) {
			try {
				CodeLookupBean lookupBean = getLookupBean(codeType);
				CodeLookupDAO dao = getDAOInstance();
				list = dao.getCodeList(lookupBean, null, null, new ArrayList());
				// Store code list into the cache.
				if (list == null) {
					list = new ArrayList();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
            if(requireEmpty&&list.size()>0){
                List l=new ArrayList();
                CodeModel codeModel=new CodeModel();
                codeModel.setCodeId("");
                codeModel.setCodeDesc("");
                l.add(codeModel);
                l.addAll(list);
                return l;
            }
            return list;
		} else {
			return new ArrayList();
		}
	}

    public List getCodeList(String codeType) throws CodeLookupException{
          return this.getCodeList(codeType,false);
    }

    public CodeModel getCode(String codeType, String codeId)
			throws CodeLookupException {
		CodeModel codeTO;
		List list=this.getCodeList(codeType);
		if(list!=null&&list.size()>0){
			for(int i=0;i<list.size();i++){
				codeTO=(CodeModel)list.get(i);
				if(codeTO.getCodeId().equalsIgnoreCase(codeId)){
					return codeTO;
				}
			}
		}
		return new CodeModel();
	}

	public String getCodeDesc(String codeType, String codeId)
			throws CodeLookupException {

		CodeModel codeTO=this.getCode(codeType, codeId);
		return codeTO.getCodeDesc();

	}

    public List getCodeListWithFilter(String codeType, String filterName,
			List filterParamList) throws CodeLookupException{
           return this.getCodeListWithFilter(codeType,filterName,filterParamList,false);
    }

    public List getCodeListWithFilter(String codeType, String filterName,
			List filterParamList,boolean requireEmpty) throws CodeLookupException {

		List codeList;
		try {
			CodeLookupBean lookupBean = getLookupBean(codeType);
			CodeLookupDAO dao = getDAOInstance();
			if (filterParamList == null) {
				filterParamList = new ArrayList();
			}
			codeList = dao.getCodeList(lookupBean, null, filterName,
					filterParamList);
		} catch (Throwable ex) {
			throw new CodeLookupException(codeType, ex);
		}
        if(requireEmpty&&codeList.size()>0){
            List l=new ArrayList();
            CodeModel codeModel=new CodeModel();
            codeModel.setCodeId("");
            codeModel.setCodeDesc("");
            l.add(codeModel);
            l.addAll(codeList);
            return l;
        }
        return codeList;
	}

	public void loadAllCodesList() throws CodeLookupException {
		try {
			CodeLookupDAO dao = getDAOInstance();
			dao.loadAllCodesList();
			CodeLookupConfig config = CodeLookupConfig.getInstance();
			List list = config.getAllLookupBean();
			List codeList = null;
			if (list != null && list.size() > 0) {
				for (int i = 0; i < list.size(); i++) {
					CodeLookupBean lookupBean = (CodeLookupBean) list.get(i);
					if (lookupBean.getCodeType().startsWith("CACHE")) {
						codeList = dao.getCodeList(lookupBean, null, null,
								new ArrayList());
						CacheManager.getInstance().putInCache(
								lookupBean.getCodeType(), codeList);
					}
				}
			}
		} catch (Throwable ex) {
			throw new CodeLookupException(ex);
		}
	}

	private CodeLookupBean getLookupBean(String codeType)
			throws CodeLookupException {

		CodeLookupConfig config = CodeLookupConfig.getInstance();
		CodeLookupBean lookupBean = config.getCodeLookup(codeType);
		if (lookupBean == null) {
			String errorMsg = "Code type[" + codeType
					+ "] is not configured in config file.";

			throw new CodeLookupException(codeType, errorMsg);
		}
		return lookupBean;
	}

}
